import request from '@/router/axios'

export const loginByUsername = (username, password) => {
  return request({
    url: '/api/oauth/token',
    headers: {
      isToken: false,
      'Authorization': 'Basic cGNTaWRlOnBjU2lkZQ=='
    },
    method: 'post',
    params: {
      grant_type: 'custom',
      handler_type: 'username',
      principal: username,
      credentials: password
    },
  })
}

export const loginByEmail = (email, code) => {
  return request({
    url: '/api/oauth/token',
    headers: {
      isToken: false,
      'Authorization': 'Basic cGNTaWRlOnBjU2lkZQ=='
    },
    method: 'post',
    params: {
      grant_type: 'custom',
      handler_type: 'email',
      principal: email,
      credentials: code
    },
  })
}

export const refreshToken = (refresh_token) => {
  return request({
    url: '/api/oauth/token',
    headers: {
      'isToken': false,
      'Authorization': 'Basic cGNTaWRlOnBjU2lkZQ=='
    },
    method: 'post',
    params: {
      refresh_token,
      grant_type: 'refresh_token'
    }
  })
}

export const loginByMobile = (mobile, code) => {
  return request({
    url: '/api/oauth/token',
    headers: {
      isToken: false,
      'Authorization': 'Basic cGNTaWRlOnBjU2lkZQ=='
    },
    method: 'post',
    params: {
      grant_type: 'custom',
      handler_type: 'phone',
      principal: mobile,
      credentials: code
    },
  })
}

export const logout = () => {
  return request({
    url: '/api/token/logout',
    method: 'delete'
  })
}
